Xceed DataGrid for WPF v7.2 Documentation
Selecting all Data Items in a Group

Welcome to Xceed DataGrid, Editors, and 3D Views for WPF v7.2 > Xceed DataGrid for WPF > Code Snippets > Selecting all Data Items in a Group

The following example demonstrates how to select all the data items contained in a group by adding them to a grid's selected-items collection when a group-header control is pressed. In the case where a group contains child groups, all the data items in the child groups will also be selected.

In v5.3 and up, this functionality can be activated by setting the IsGroupSelectionEnabled property to true.
XAML
Copy Code
  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
    <Grid.Resources>
      <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                    Source="{Binding Source={x:Static Application.Current},
                                                        Path=Orders}">
        <xcdg:DataGridCollectionViewSource.GroupDescriptions>
          <xcdg:DataGridGroupDescription PropertyName="ShipCountry"/>
          <xcdg:DataGridGroupDescription PropertyName="ShipCity"/>
        </xcdg:DataGridCollectionViewSource.GroupDescriptions>
      </xcdg:DataGridCollectionViewSource>
    <Style TargetType="{x:Type xcdg:GroupHeaderControl}">
      <EventSetter Event="PreviewMouseLeftButtonDown"
                   Handler="SelectRows"/>
    </Style>
  </Grid.Resources>
    <xcdg:DataGridControl x:Name="OrdersGrid"
                        ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
  </Grid>
VB.NET
Copy Code
Private Sub SelectRows( ByVal sender As Object, ByVal e As MouseButtonEventArgs )
  Dim groupHeaderControl As GroupHeaderControl = TryCast( sender, GroupHeaderControl );
  If groupHeaderControl Is Nothing Then
    Return;
  End If
  Dim context As DataGridContext = DataGridControl.GetDataGridContext( groupHeaderControl )
  Dim header As GroupHeaderFooterItem = TryCast( context.GetItemFromContainer( grouHeaderControl, GroupHeaderFooterItem )
  If Not header.Equals( GroupHeaderFooterItem.Empty ) Then
    Dim group As CollectionViewGroup = context.GetParentGroupFromItem( header )
    If Not group Is Nothing Then
      For Each item As Object In group.Items
        If TypeOf item Is CollectionViewGroup Then
          Me.SelectRecurse( context, CType( item, CollectionViewGroup ) )
        Else
          context.SelectedItems.Add( item )
        End If
      Next item
    End If
  End If
End Sub
Private Sub SelectRecurse( ByVal context As DataGridContext, ByVal group As CollectionViewGroup )
  For Each item As Object In group.Items
    If TypeOf item Is CollectionViewGroup Then
      Me.SelectRecurse( context, CType( item, CollectionViewGroup ) )
    Else
      context.SelectedItems.Add( item )
    End If
  Next item
End Sub
C#
Copy Code
private void SelectRows( object sender, MouseEventArgs e )
{
GroupHeaderControl groupHeaderControl = sender as GroupHeaderControl;
if( groupHeaderControl == null )
  return;
DataGridContext context = DataGridControl.GetDataGridContext( groupHeaderControl );
GroupHeaderFooterItem header = ( GroupHeaderFooterItem )context.GetItemFromContainer( groupHeaderControl );
if( !header.Equals( GroupHeaderFooterItem.Empty ) )
{
  CollectionViewGroup group = context.GetParentGroupFromItem( header );
  if( group != null )
  {
    foreach( object item in group.Items )
    {
      if( item is CollectionViewGroup )
      {
        this.SelectRecurse( context, ( CollectionViewGroup )item );
      }
      else
      {
        context.SelectedItems.Add( item );
      }
    }
  }
}
}
private void SelectRecurse( DataGridContext context, CollectionViewGroup group )
{
foreach( object item in group.Items )
{
  if( item is CollectionViewGroup )
  {
    this.SelectRecurse( context, ( CollectionViewGroup )item );
  }
  else
  {
    context.SelectedItems.Add( item );
  }
}
}